package com.weight.system.sensor.web;

import com.weight.system.sensor.PO.SensorInfo;
import com.weight.system.sensor.VO.SensorInfoVO;
import com.weight.system.sensor.service.SensorInfoService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import com.weight.common.util.JsonResultUtil;
import com.weight.common.util.CommonUtils;
import com.weight.common.util.DateFormUtils;
import com.weight.common.model.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import com.weight.common.enums.ResultEnum;

/**
 * 传感器错误标记表
 */
@RestController
@Slf4j
@RequestMapping("/sensor")
public class SensorInfoController {

	@Autowired
	private SensorInfoService sensorInfoService;

	private String pattern = "yyyy-MM-dd";


	/**
	 * 1.查询传感器错误标记表--列表页 
	 * @return
	 */
	@RequestMapping(value="/queryForSensorInfoList",method=RequestMethod.GET)
	public ModelAndView queryForSensorInfoList(){
			ModelAndView modelAndView = new ModelAndView();
			modelAndView.setViewName("sensor/queryForSensorInfoList");
			
			Map<Object, Object> map =new HashMap<>();
			map.put("sensorStatue", 1);

			
			//2、查询传感器错误标记表列表
			List<SensorInfo> sensorInfoList = sensorInfoService.findSensorInfoByT(map);
			log.info("sensorInfoList={}",sensorInfoList);
			 
			
			modelAndView.addObject("sensorInfoList", sensorInfoList);
			return modelAndView;
	}

	/**
	 * 2.查询传感器错误标记表--数据刷新  
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/queryForSensorInfoListDG",method=RequestMethod.GET)
	public String queryForSensorInfoListDG(HttpServletRequest request){
			//1、获取查询条件
			Integer page = Integer.parseInt(request.getParameter("page"));
			Integer rows = Integer.parseInt(request.getParameter("rows"));
			
			String sensorNum = request.getParameter("sensorNum");
			String sensorDesc = request.getParameter("sensorDesc");
			String sensorStatue = request.getParameter("sensorStatue");
			String sensorDate = request.getParameter("sensorDate");

			Map<Object, Object> map =new HashMap<>();
			map.put("sensorNum", sensorNum);
			map.put("sensorDesc", sensorDesc);
			map.put("sensorStatue", sensorStatue);
			map.put("sensorDate", DateFormUtils.getStringToDate(sensorDate, pattern));

			
			//2、查询传感器错误标记表列表
			List<SensorInfo> sensorInfoList = sensorInfoService.findSensorInfoByPage(page,rows,map);
			log.info("sensorInfoList={}",sensorInfoList);
			List<SensorInfoVO> sensorInfoVOList = sensorInfoList.stream().map(e -> changePoToVO(e)).collect(Collectors.toList());
			//3、返回前台
			PageResult<SensorInfoVO> result = new PageResult<SensorInfoVO>(sensorInfoVOList);
			return  JSON.toJSONString(result);
	}

	/**
	 * 展示模板--转--保存模板  
	 * @param sensorInfoVO
	 * @param sensorInfo
	 * @return
	 */
	public void changeVOToPO(SensorInfoVO sensorInfoVO,SensorInfo sensorInfo){
			BeanUtils.copyProperties(sensorInfoVO,sensorInfo);
			sensorInfo.setSensorDate(DateFormUtils.getStringToDate(sensorInfoVO.getSensorDate(), pattern));
			

	}

	/**
	 * 保存模板--转--展示模板  
	 * @param sensorInfoVO
	 * @param sensorInfo
	 * @return
	 */
	public void changePoToVO(SensorInfoVO sensorInfoVO,SensorInfo sensorInfo){
			BeanUtils.copyProperties(sensorInfo,sensorInfoVO);
			sensorInfoVO.setSensorDate(DateFormUtils.getDateToString(sensorInfo.getSensorDate(), pattern));
			

	}

	/**
	 * 保存模板--转--展示模板  
	 * @param sensorInfo
	 * @return
	 */
	public SensorInfoVO changePoToVO(SensorInfo sensorInfo){
			SensorInfoVO sensorInfoVO=new SensorInfoVO();
			BeanUtils.copyProperties(sensorInfo,sensorInfoVO);
			sensorInfoVO.setSensorDate(DateFormUtils.getDateToString(sensorInfo.getSensorDate(), pattern));
			

			return sensorInfoVO;
	}

	/**
	 * 3.查询传感器错误标记表--详情页
	 * @param sensorInfoId
	 * @return
	 */
	@RequestMapping("/queryForSensorInfoDetail")
	public ModelAndView queryForSensorInfoDetail(@RequestParam(value="sensorInfoId",required=false) String sensorInfoId){
			//1.校验id
			CommonUtils.validateExistID(sensorInfoId);
			
			//2.传参，查询
			Map<String, String> map=new HashMap<>();
			map.put("sensorInfoId", sensorInfoId);
			log.info("查询传感器错误标记表入参，map={}",map);
			ModelAndView modelAndView = new ModelAndView();
			SensorInfo sensorInfo = sensorInfoService.findBySensorInfoId(map);
			
			SensorInfoVO sensorInfoVO = changePoToVO(sensorInfo);
			//3、返回前台
			log.info("查询传感器错误标记表查询成功：sensorInfo={}",sensorInfo);
			modelAndView.setViewName("/system/sensor/queryForSensorInfoDetail");
			modelAndView.addObject("sensorInfoVO",sensorInfoVO);
			modelAndView.addObject("formID",CommonUtils.getUUId());
			return modelAndView;
	}

	/**
	 * 3.添加、修改传感器错误标记表操作
	 * @param sensorInfoId
	 * @return
	 */
	@RequestMapping(value="/addSensorInfo")
	public ModelAndView addSensorInfo(@RequestParam(value="sensorInfoId",required=false) String sensorInfoId){
			//1、创建返回对象
			SensorInfoVO sensorInfoVO = new SensorInfoVO();
			
			//2.判定id不为空，则查询数据库，为修改操作
			if(StringUtils.isNotBlank(sensorInfoId)) {
				Map<String, String> map=new HashMap<>();
				map.put("sensorInfoId", sensorInfoId);
				log.info("修改操作，查询ID：sensorInfoId={}",sensorInfoId);
				SensorInfo sensorInfo = sensorInfoService.findBySensorInfoId(map);
				this.changePoToVO(sensorInfoVO,sensorInfo);
			}
			
			//3.数据返回给前台
			ModelAndView modelAndView = new ModelAndView();
			modelAndView.setViewName("/system/sensor/addSensorInfo");
			modelAndView.addObject("sensorInfoVO",sensorInfoVO);
			modelAndView.addObject("formID",CommonUtils.getUUId());
			return modelAndView;
	}

	/**
	 * 5.保存 添加、修改传感器错误标记表操作
	 * @param sensorInfoVO
	 * @param bindingResult
	 * @return
	 */
	@Transactional
	@RequestMapping(value="/saveSensorInfo")
	public JsonResultUtil saveSensorInfo(@Valid SensorInfoVO sensorInfoVO,BindingResult bindingResult){
			String operateStr="add";
			//1.校验入参规则
			CommonUtils.validateBindingResult(bindingResult,sensorInfoVO);
			
			//2.获取id
			String sensorInfoId=sensorInfoVO.getSensorInfoId();
			SensorInfo sensorInfo=new SensorInfo();
			BeanUtils.copyProperties(sensorInfoVO,sensorInfo);
			this.changeVOToPO(sensorInfoVO,sensorInfo);
			
			//3.判定id不为空，则为修改操作
			if(StringUtils.isNotBlank(sensorInfoId)) {
				operateStr="edit";
				Map<String, String> map=new HashMap<>();
				map.put("sensorInfoId", sensorInfoId);
				log.info("修改操作，查询ID：sensorInfoId={}",sensorInfoId);
				//查询是否存在
				sensorInfoService.findBySensorInfoId(map);
				try{
					sensorInfo.setUpdateBy("000");
					sensorInfoService.updateSensorInfo(sensorInfo);
					log.info("更新操作成功：sensorInfo={}",sensorInfo);
				}catch(Exception e){
					e.printStackTrace();
					log.error(e.getMessage(), e);
					return new JsonResultUtil(ResultEnum.EXCEPTION, "失败！", e.getMessage());
				}
			}else {//判定id为空，则为添加操作
				try{
					sensorInfo.setSensorInfoId(CommonUtils.getUUId());
					sensorInfo.setCreateBy("000");
					sensorInfo.setUpdateBy("000");
					sensorInfoService.insertSensorInfo(sensorInfo);
					log.info("添加操作成功：sensorInfo={}",sensorInfo);
				}catch(Exception e){
					e.printStackTrace();
					log.error(e.getMessage(), e);
					return new JsonResultUtil(ResultEnum.EXCEPTION, "失败！", e.getMessage());
				}
			}
			return new JsonResultUtil(ResultEnum.SUCCESS, "操作成功！", operateStr);
	}

	/**
	 * 6.删除传感器错误标记表操作
	 * @param sensorInfoId
	 * @return
	 */
	@Transactional
	@RequestMapping("/deleteBySensorInfoId")
	public JsonResultUtil deleteBySensorInfoId(@RequestParam(value="sensorInfoId",required=false) String sensorInfoId){
			//1.校验id
			CommonUtils.validateExistID(sensorInfoId);
			
			//2.校验数据库中是否存在!
			Map<String, String> map=new HashMap<>();
			map.put("sensorInfoId", sensorInfoId);
			sensorInfoService.findBySensorInfoId(map);
			//3.传参,添加操作人 删除
			map.put("updateBy","000");
			log.info("删除传感器错误标记表入参，map={}",map);
			
			try{
				sensorInfoService.upDelBySensorInfoId(map);
				return new JsonResultUtil(ResultEnum.SUCCESS, "删除成功！", null);  
			}catch(Exception e){
				e.printStackTrace();
				log.error(e.getMessage(), e);
				return new JsonResultUtil(ResultEnum.EXCEPTION, "操作失败！", e.getMessage());
			}
	}

	/**
	 * 6.删除传感器错误标记表操作---批量删除
	 * @param sensorInfoIdList
	 * @return
	 */
	@Transactional
	@RequestMapping("/deleteBySensorInfoIdBatch")
	public JsonResultUtil deleteBySensorInfoIdBatch(@RequestParam(value="sensorInfoIdList",required=false) List sensorInfoIdList){
			//1.校验id
			CommonUtils.validateExistIDList(sensorInfoIdList);
			
			//2.校验数据库
			sensorInfoService.findBySensorInfoIdList(sensorInfoIdList);
			//3.传参 
			Map<String, Object> map=new HashMap<>();
			map.put("sensorInfoIdList", sensorInfoIdList);
			map.put("updateBy","000");
			log.info("批量删除传感器错误标记表操作入参，map={}",map);
			
			try{
				sensorInfoService.upDelBySensorInfoIdBatch(map);
				return new JsonResultUtil(ResultEnum.SUCCESS, "批量删除成功！", null);  
			}catch(Exception e){
				e.printStackTrace();
				log.error(e.getMessage(), e);
				return new JsonResultUtil(ResultEnum.EXCEPTION, "操作失败！", e.getMessage());
			}
	}

}